package com.feng.zhihao.apigeteway.util;

import java.io.IOException;
import java.io.InputStream;

import com.feng.zhihao.apigeteway.cache.CacheManager;
import com.feng.zhihao.apigeteway.cache.CacheUtil;
import com.feng.zhihao.apigeteway.cache.Element;
import com.feng.zhihao.apigeteway.cache.impl.MemoryCache;
import com.xiaoleilu.hutool.io.IoUtil;
import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

public class SQLUtil {
	static Log log=LogFactory.get();
	public final static String SELECT_CHANNEL_PROJECT_LIST="SELECT_CHANNEL_PROJECT_LIST.sql";
	public final static String SELECT_CHANNEL_PROJECT_MODULAR_LIST="SELECT_CHANNEL_PROJECT_MODULAR_LIST.sql";
	public final static String SELECT_CHANNEL_PROJECT_MODULAR_MODULAR_LIST="SELECT_CHANNEL_PROJECT_MODULAR_MODULAR_LIST.sql";
	
	static final CacheManager cacheManager = CacheUtil.createReloadCache("SQL",new MemoryCache(0, 0),(cache)->{
		InputStream in = SQLUtil.class.getResourceAsStream("/sql/"+SELECT_CHANNEL_PROJECT_LIST);
		try {
			cache.put(new Element(SELECT_CHANNEL_PROJECT_LIST, IoUtil.read(in, "UTF-8"), 0, 0));
			in = SQLUtil.class.getResourceAsStream("/sql/"+SELECT_CHANNEL_PROJECT_MODULAR_LIST);
			cache.put(new Element(SELECT_CHANNEL_PROJECT_MODULAR_LIST, IoUtil.read(in, "UTF-8"), 0, 0));
			in = SQLUtil.class.getResourceAsStream("/sql/"+SELECT_CHANNEL_PROJECT_MODULAR_MODULAR_LIST);
			cache.put(new Element(SELECT_CHANNEL_PROJECT_MODULAR_MODULAR_LIST, IoUtil.read(in, "UTF-8"), 0, 0));
		} catch (IOException e) {
			log.error("",e);
		} finally {
			try {
				in.close();
			} catch (IOException e) {
				log.error("",e);
			}
		}
	});
	public static String getSELECT_CHANNEL_PROJECT_LIST(){
		return (String) cacheManager.getValue(SELECT_CHANNEL_PROJECT_LIST).getValue();
	}
	public static String getSELECT_CHANNEL_PROJECT_MODULAR_LIST(){
		return (String) cacheManager.getValue(SELECT_CHANNEL_PROJECT_MODULAR_LIST).getValue();
	}
	public static String getSELECT_CHANNEL_PROJECT_MODULAR_MODULAR_LIST(){
		return (String) cacheManager.getValue(SELECT_CHANNEL_PROJECT_MODULAR_MODULAR_LIST).getValue();
	}
}
